﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

public partial class ProductManage_BothProductManage_ClientSoftManage : System.Web.UI.Page
{
    private ClientSoft cs;

    public ProductManage_BothProductManage_ClientSoftManage()
    {
        cs = new ClientSoft();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindClientSoft();
        }
    }

    protected void BindClientSoft()
    {
        this.GridView1.DataSource = cs.LoadEntities();
        this.GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindClientSoft();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindClientSoft();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        if (((DropDownList)e.Row.FindControl("DDLIsValid")) != null)
        {
            DropDownList ddlIsValid = (DropDownList)e.Row.FindControl("DDLIsValid");
            ddlIsValid.SelectedValue = ((HiddenField)e.Row.FindControl("HDFIsValid")).Value;
        }
        if (((DetailsView)e.Row.FindControl("DetailsView1")) != null)
        {
            DetailsView d = (DetailsView)e.Row.FindControl("DetailsView1");
            if (((DropDownList)d.Rows[1].Cells[1].FindControl("DDLCSName")) != null)
            {
                DropDownList ddlCSName = (DropDownList)d.Rows[1].Cells[1].FindControl("DDLCSName");
                ClientSoftware csw = new ClientSoftware();
                ddlCSName.DataSource = csw.LoadEntities();
                ddlCSName.DataTextField = "CSName";
                ddlCSName.DataValueField = "CSName";
                ddlCSName.DataBind();
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int cSID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        cs.Delete(cSID);
        BindClientSoft();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindClientSoft();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ClientSoftInfo csInfo = new ClientSoftInfo();
        csInfo.CSID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        csInfo.CSName = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        csInfo.CSVersion = GridView1.DataKeys[e.RowIndex].Values[2].ToString();
        csInfo.Remark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        csInfo.IsValid = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLIsValid"))).SelectedValue);

        cs.Update(csInfo);
        GridView1.EditIndex = -1;
        BindClientSoft();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ClientSoftInfo csInfo = new ClientSoftInfo();
        csInfo.CSID = 0;
        csInfo.CSName = ((DropDownList)(d.Rows[0].Cells[1].FindControl("DDLCSName"))).SelectedValue;
        csInfo.CSVersion = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        csInfo.Remark = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
        csInfo.IsValid = bool.Parse(((DropDownList)(d.Rows[3].Cells[1].FindControl("DDLIsValid"))).SelectedValue);

        ClientSoftInfo csInfo1 = cs.GetClientSoftbyNV(csInfo.CSName, csInfo.CSVersion);
        if (!string.IsNullOrEmpty(csInfo1.CSName))
        {
            string str1 = "添加不了啦，此客户端软件已经存在此版本软件！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            cs.Insert(csInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindClientSoft();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindClientSoft();
    }
}
